package com.yonyou.crm.base.doc.ctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.base.doc.entity.BaseDocDetailVO;
import com.yonyou.crm.base.doc.entity.BaseDocVO;
import com.yonyou.crm.base.doc.rmitf.IBaseDocRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;

@RestController
@RequestMapping(value = "/base/docs")
public class BaseDocCtrl {
	
	@Autowired
	private IBaseDocRmService service;
	
	/**
	 * 档案列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object queryBaseDocList(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		if(paramObj == null){
			Page<BaseDocVO> baseDocPage = service.getBaseDocPage(new Page<BaseDocVO>(
					0, 0), null);
			return ResponseUtil.getObject(baseDocPage);
		}else{
			@SuppressWarnings("unchecked")
			Map<String, Object> condMap = (Map<String, Object>) paramObj.get("searchMap");
			//本页面不进行分页查询，page参数在mapper中不使用
			Page<BaseDocVO> baseDocPage = service.getBaseDocPage(getRequestPage(paramObj), condMap);
			return ResponseUtil.getObject(baseDocPage);
		}
	}
	
	private Page<BaseDocVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {
		
		Integer page = paramObj.getInt(HttpKeyConstant.Page);
		Integer pageSize = paramObj.getInt(HttpKeyConstant.PageSize);
		
		if (page == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}
		
		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}
		
		return new Page<BaseDocVO>(page, pageSize);
	}

	/**
	 * 新增档案
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Class<?>> fieldClassMap = new HashMap<String, Class<?>>();
		fieldClassMap.put("baseDocDetailList", BaseDocDetailVO.class);
		BaseDocVO baseDoc = RequestUtil.getVO(request, BaseDocVO.class,fieldClassMap);
		baseDoc = service.insertBaseDoc(baseDoc);
		return ResponseUtil.getObject(baseDoc);
	}
	
	/**
	 * 批量删除档案
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object delete(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idArray = paramObj.getString("ids");		

		if (idArray == null) {
			throw new CrmMessageException("ids参数为空");
		}
		String[] idStrs = idArray.split(",");
		@SuppressWarnings("unchecked")
		Map<String, Object> searchMap = (Map<String, Object>)paramObj.get("searchMap");
		Page<BaseDocVO> baseDocPage = service.deleteBaseDocByIDs(idStrs, getRequestPage(paramObj), searchMap);
		return ResponseUtil.getObject(baseDocPage, PCCodeConstant.Success,"数据删除成功");
	}
	
	/**
	 * 编辑档案
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id,
			HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		Map<String, Class<?>> fieldClassMap = new HashMap<String, Class<?>>();
		fieldClassMap.put("baseDocDetailList", BaseDocDetailVO.class);
		BaseDocVO baseDoc = RequestUtil.getVO(request, BaseDocVO.class,fieldClassMap);	
		if (baseDoc == null) {
			throw new CrmMessageException("id不能为空");
		}
		baseDoc.setId(id);
		baseDoc = service.updateBaseDoc(baseDoc);
		return ResponseUtil.getObject(baseDoc);
	}
	
	/**
	 * 档案详情
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Object getBaseDocDetailList(@PathVariable("id") Long id, 
			HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		return ResponseUtil.getObject(service.detail(id));
	}
	
	/**
	 * 根据ID数组返回档案信息和档案详情
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "detail", method = RequestMethod.GET)
	public Object getDocAndDetailList(HttpServletRequest request, HttpServletResponse response) 
			throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idArray = null;
		if(paramObj != null){
			idArray = paramObj.getString("ids");		
		}

		JSONObject result = new JSONObject();
		if (idArray == null) {
			List<BaseDocVO> baseDocList = service.getDocByIds(null);
			result.put("data", baseDocList);
			return ResponseUtil.getObject(result);
		}
		String[] idStrs = idArray.split(",");
		List<BaseDocVO> baseDocList = service.getDocByIds(idStrs);
		result.put("data", baseDocList);
		return ResponseUtil.getObject(result);
	}
}
